package cn.itcast.aspect;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDateTime;

@Aspect
@Component
@Slf4j
public class CalculateRunTimeAdvice {

    @Pointcut("execution(* cn.itcast.service.*Service.*(..))")
    private void pc() {
    }

    //@Around("@annotation(cn.itcast.aspect.MyLog)")
    @Around("pc()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        LocalDateTime start = LocalDateTime.now();
        Object result = pjp.proceed();
        LocalDateTime end = LocalDateTime.now();
        Signature signature = pjp.getSignature();
        String methodName = signature.getName();
        String className = signature.getDeclaringTypeName();
        Duration between = Duration.between(start, end);
        long time = between.toNanos();
        long newTime = time / 1000000;
        log.info("{}中的{}执行了:{}纳秒~,也就是{}毫秒", className, methodName, time, newTime);
        return result;
    }
}
